var Web3 = require("web3");
var Tx = require('ethereumjs-tx');
const keythereum = require("keythereum");

// 创建web3对象
var web3 = new Web3();
// 连接到以太坊节点
web3.setProvider(new Web3.providers.HttpProvider("http://192.168.1.89:8545"));
// web3.setProvider(new Web3.providers.HttpProvider("http://120.79.114.111:8546"));

var abi = [{
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [{"name": "", "type": "string"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "decimals",
    "outputs": [{"name": "", "type": "uint8"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [{"name": "", "type": "address"}],
    "name": "balanceOf",
    "outputs": [{"name": "", "type": "uint256"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": true,
    "inputs": [],
    "name": "symbol",
    "outputs": [{"name": "", "type": "string"}],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
}, {
    "constant": false,
    "inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}],
    "name": "transfer",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}, {
    "inputs": [{"name": "_supply", "type": "uint256"}, {"name": "_name", "type": "string"}, {
        "name": "_symbol",
        "type": "string"
    }, {"name": "_decimals", "type": "uint8"}, {"name": "owner", "type": "address"}],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
}, {
    "anonymous": false,
    "inputs": [{"indexed": true, "name": "from", "type": "address"}, {
        "indexed": true,
        "name": "to",
        "type": "address"
    }, {"indexed": false, "name": "value", "type": "uint256"}],
    "name": "Transfer",
    "type": "event"
}];

var contractAddress = "0x3c7842a3269f0049e069fd720c442785ec35afe2";
// contractAddress="0xe6a382df08e7b9a056b7f30e8eb3c18dac973471";
var from = web3.eth.accounts[1];
var keyObject = {
    "address": "471912d13bbe0a91aaa553af4af048885943bd61",
    "crypto": {
        "cipher": "aes-128-ctr",
        "ciphertext": "b6a18faaa6174a1959cf7e106ddfebee6ee34651115ac9313be25d82ef5aa8a1",
        "cipherparams": {"iv": "65d705bb1c3c50662b5cb894e32abb9b"},
        "kdf": "scrypt",
        "kdfparams": {
            "dklen": 32,
            "n": 262144,
            "p": 1,
            "r": 8,
            "salt": "f264c0f89b48188bf933cdbd00c66e2581a178e6036a938d894d8ef6bba3d3e2"
        },
        "mac": "f9cc8d74b991f2e58ddd1a14d7c99d8b2688514d43befeadf4e4b8611fd5a7c5"
    },
    "id": "43276613-4052-42b3-b99e-96d0630094e1",
    "version": 3
}
var privKey = keythereum.recover("123456789", keyObject)
// var privKey = new Buffer(privateKey, 'hex');
var to = web3.eth.accounts[0];
var contract = web3.eth.contract(abi).at(contractAddress);


console.log("转帐前帐户From" + contract.balanceOf(from).toNumber())
console.log("转帐前帐户To" + contract.balanceOf(to).toNumber())

var count = web3.eth.getTransactionCount(from);
var data = contract.transfer.getData(to, 20);
var rawTransaction = {
    "from": from,
    "nonce": web3.toHex(count),
    "gasPrice": "0x04e3b29200",
    "gasLimit": 60000,
    "to": contractAddress,
    "value": "0x0",
    "data": data,
    "chainId": 123456
};

var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
var txHash = "";
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {
    if (!err) {
        txHash = hash;
        console.log(hash)
    }
    else {
        console.log(err);
    }

});

// function xxx() {
//     console.log(txHash)
//     var receipt = web3.eth.getTransactionReceipt(txHash);
//     console.log(receipt)
//     if (!receipt || receipt.status != "0x1") {
//         setTimeout(xxx, 2000)
//     } else {
//         console.log("转帐后帐户From" + contract.balanceOf(from).toNumber())
//         console.log("转帐后帐户To" + contract.balanceOf(to).toNumber())
//     }
// }





